using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using UnityEngine;

public class AStarText : MonoBehaviour
{
    public int beingX = 0;
    public int beingY = 0;

    public float offSetX = 1.2f;
    public float offSetY = 1.2f;

    public int mapW = 5;
    public int mapH = 5;

    public Dictionary<string, GameObject> dic = new Dictionary<string, GameObject>();
    public List<AStarNode> list;

    public Vector2 beginPos = Vector2.right * -1;

    // Start is called before the first frame update
    void Start()
    {
        AStarMgr.Instance.InitMapInfo(mapW, mapH);
        for (int i = 0; i < mapW; i++)
        {
            for (int j = 0; j < mapH; j++)
            {
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                cube.transform.position = new Vector3(i * offSetX, 0, j * offSetY);

                cube.name = i + "_" + j;
                dic.Add(cube.name, cube);

                AStarNode node = AStarMgr.Instance.nodes[i, j];
                if (node.type == E_Node_Type.Stop)
                {
                    cube.GetComponent<Renderer>().material.color = Color.red;
                }
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, 1000))
            {

                if (beginPos == Vector2.right * -1)
                {
                    //清理上一次的路径
                    if (list != null)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            dic[list[i].x + "_" + list[i].y].GetComponent<Renderer>().material.color = Color.white;
                        }
                    }

                    string[] str = hit.collider.name.Split('_');
                    beginPos = new Vector2(int.Parse(str[0]), int.Parse(str[1]));
                    hit.collider.GetComponent<Renderer>().material.color = Color.yellow;
                }
                else
                {
                    string[] str = hit.collider.name.Split('_');
                    Vector2 endpos = new Vector2(int.Parse(str[0]), int.Parse(str[1]));

                    //寻路
                    list = AStarMgr.Instance.FindPath(beginPos, endpos);
                    dic[(int)beginPos.x + "_" + (int)beginPos.y].GetComponent<Renderer>().material.color = Color.white;
                    if (list != null)
                    {
                        for (int i = 0; i < list.Count; i++)
                        {
                            dic[list[i].x + "_" + list[i].y].GetComponent<Renderer>().material.color = Color.green;
                        }
                    }
                    beginPos = Vector2.right * -1;
                }

            }
        }
    }
}
